package jdbc.homework01;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DbUtil {
	private static String url = "jdbc:mysql://localhost:3306/java_mysql_homework01?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
	private static String user = "root";
	private static String passworld = "root";

	// 构造方法私有化 不给 new
	private DbUtil() {
	}

	public static Connection getConnection() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			System.out.println("没有找到驱动");
			e.printStackTrace();
		}
		Connection con = null;
		try {
			con = DriverManager.getConnection(url, user, passworld);
			return con;
		} catch (SQLException e) {

			e.printStackTrace();
		}
		return null;
	}

	public static void close(Connection con, Statement st, ResultSet rs, PreparedStatement ps) {
		close(rs);
		close(st);
		close(ps);
		close(con);

	}

	private static void close(Connection con) {
		if (con != null)
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

	}

	private static void close(ResultSet rs) {
		if (rs != null)
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

	}

	private static void close(PreparedStatement ps) {

		if (ps != null)
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
	}

	private static void close(Statement st) {

		if (st != null)
			try {
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
	}

}
